package com.christiancantrell.extensions
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.StatusEvent;
	import flash.external.ExtensionContext;
	
	/**
	 * url : https://github.com/cantrell/CompassExample
	 * @author github
	 * 
	 */	
	public class Compass extends EventDispatcher
	{
		
		private var exContext:ExtensionContext;
		
		
		
		/**
		 * github - compas
		 * <br />
			this.compass = new Compass();
			<br />
			// 등록
			this.compass.register();
			<br />
			// 제거시
//			this.compass.deregister();
			<br />
			this.compass.addEventListener(StatusEvent.STATUS, onStatusUpdate);
			 * 
			 * <br /><br /><br />
			 * 
			 *  
			 * protected function onStatusUpdate(e:StatusEvent):void<br />
		{<br />
			var values:Array = e.level.split("&");<br />
			var azimuth:Number = Number(values[0]);<br />
			this.lastAz = ((azimuth > 180) ? azimuth - 360 : azimuth) * -1;<br />
			
			trace(" ==> View16_Geolocation onStatusUpdate : " + lastAz,  Number(values[0]) );<br />
			
		}		<br />
		 * 
		 * lastAz 는 -180 ~ 180 각도를 의미함.
		 * <br />
		 * <br />
		 * <br />
		 * @param target
		 * 
		 */		
		public function Compass(target:IEventDispatcher=null)
		{
			super(target);
			this.exContext = ExtensionContext.createExtensionContext("com.christiancantrell.compass","compass");
		}
		
		
		/**
		 * 사용 등록 
		 */		
		public function register():void
		{
			this.exContext.call("register");
			this.exContext.addEventListener(StatusEvent.STATUS, onStatusChange);
		}
		
		/**
		 * 제거 
		 */		
		public function deregister():void
		{
			this.exContext.call("deregister");
			this.exContext.removeEventListener(StatusEvent.STATUS, onStatusChange);
		}
		
		
		
		/**
		 * 상태변화 이벤트 반환
		 * @param e : StatusEvent
		 */		
		private function onStatusChange(e:StatusEvent):void
		{
			if (e.code == "MAGNETIC_FIELD_CHANGED")
			{
				this.dispatchEvent(e);
			}
		}
	}
}


